package application3.validator;

import common.form.ProductForm;

import java.util.ArrayList;
import java.util.List;

/**
 * Product 类的校验器
 */
public class ProductValidator {

    public List<String> validate(ProductForm productForm) {
        List<String> errors = new ArrayList<String>();
        String name = productForm.getName();
        if (name.trim().length() == 0 || name == null) {
            errors.add("产品名称为空");
        }

        String price = productForm.getPrice();
        if (price == null || price.trim().length() == 0) {
            errors.add("产品价格为空");
        } else {
            try {
                Float.parseFloat(price);
            } catch (NumberFormatException e) {
                errors.add("产品价格不为数字类型" + e.toString());
            }
        }

        return errors;
    }

}
